package com.xlro.service;

import com.xlro.dao.BookDao;
import com.xlro.domain.Book;
import com.xlro.domain.BorrowBooks;
import com.xlro.domain.BrowseBook;
import com.xlro.domain.CollBook;

import java.util.ArrayList;

public class BookService {

    BookDao bookDao = new BookDao();

    //    获取所有图书
    public ArrayList<Book> getAllBooks() {

        ArrayList<Book> books = bookDao.getAllBook();
        return books;

    }

//    获取图书
    public Book getBook(String id) {
        ArrayList<Book> books = this.getAllBooks();
        Book boo = null;
        for (Book b:books
             ) {
            if(id.equals(b.getId())){
                boo = b;
            }
        }
        return boo;
    }

//    返回记录结果
    public String setBrowse(String bookId, String username, String thisTime) {

        String userId = new UserService().getUser(username).getId();
        return bookDao.insertBrowse(bookId,userId,thisTime)>0?"成功":"失败";
    }




//    借阅图书
    public String book(String card_id,String book_id,String borrow_date,String end_date){
        int i = bookDao.insertBorrowBooks(card_id, book_id, borrow_date, end_date);

//        **************************************************
//        **************************************************
//        i == 0  未查询到
//        i < 2   插入成功
//        i >= 2  已经有两条数据了
//        **************************************************
        return i>0?i<2?"借阅成功":"看完当前所借阅的两本再来！":"借阅失败";
    }

    public ArrayList<BrowseBook> getBrowseBook(String userId) {
        return bookDao.selectBrowseBook(userId);
    }

//    收藏
    public String collUncoll(String userId, String bookId, String aThis) {

        return bookDao.alterCollBook(userId, bookId, aThis);

    }

    public String delAllColBook(String userId) {
        return bookDao.delAllColBook(userId);
    }

    public ArrayList<CollBook> getCollect(String userId) {
        return bookDao.collBookRecord(userId);
    }

    public ArrayList<BorrowBooks> getBorrowBook(String userId) {
        return bookDao.selectBorrowBook(userId);
    }

    public String addOneBook(String name, String author, String library, String sort, String position, String status, String desc) {
        return bookDao.addOneBook(name,author,library,sort,position,status,desc)>0?"新建成功":"失败";
    }

    public String deleteBookById(String id) {
        return bookDao.deleteBookById(id)>0?"删除图书成功":"删除图书失败";
    }

    public String updateBookById(String id, String name, String author, String library, String sort, String position, String status, String desc) {
        return bookDao.updateBookById(id,name,author,library,sort,position,status,desc)>0?"修改成功":"修改失败";
    }
}
